package com.itg.vms.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itg.vms.entity.User;
import com.itg.vms.util.AjaxResult;
import com.itg.vms.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * servlet的通用处理
 *
 * @author zjzaki
 * @create 2023年03月04日 01:30:34
 */
public class BaseServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        //得到方法名
        String methodName = req.getParameter("method");
        if ("".equals(methodName) || methodName == null) {
            resp.getWriter().write(error("无效的请求"));
            return;
        }

        try {
            Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 得到登录的用户
     */
    public User getLoginUser(HttpServletRequest req, HttpServletResponse resp) {
        //在session得到当前登录的用户
        User loginUser = (User) req.getSession().getAttribute("loginUser");
        return loginUser;
    }

    /**
     * 获取登录用户的id
     */
    public Long getUserId(HttpServletRequest req, HttpServletResponse resp) {
        return getLoginUser(req, resp).getUserId();
    }

    /**
     * 获取登录的用户名
     */
    public String getUsername(HttpServletRequest req, HttpServletResponse resp) {
        return getLoginUser(req, resp).getUserName();
    }

    /**
     * 将对象装换为JSON字符串
     *
     * @param obj
     * @return
     */
    private String obj2Json(Object obj) {
        return JSONObject.toJSONStringWithDateFormat(obj, JSON.DEFFAULT_DATE_FORMAT);
    }

    /**
     * 返回成功
     */
    public String success() {
        return obj2Json(AjaxResult.success());
    }

    /**
     * 返回成功消息
     */
    public String success(String message) {
        return obj2Json(AjaxResult.success(message));
    }

    /**
     * 返回成功消息
     */
    public String success(Object data) {
        return obj2Json(AjaxResult.success(data));
    }

    public String success(int code, String msg, Long count, Object data) {
        return obj2Json(AjaxResult.success(code, msg, count, data));
    }

    /**
     * 返回成功消息, 前端需要携带总记录数
     */
    public String success(Object data, Long count) {
        return obj2Json(AjaxResult.success("查询成功", data, count));
    }


    /**
     * 返回失败消息
     */
    public String error() {
        return obj2Json(AjaxResult.error());
    }

    /**
     * 返回失败消息
     */
    public String error(String message) {
        return obj2Json(AjaxResult.error(message));
    }

    /**
     * 返回警告消息
     */
    public String warn(String message) {
        return obj2Json(AjaxResult.warn(message));
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected String toAjax(int rows) {
        return rows > 0 ? obj2Json(AjaxResult.success()) : obj2Json(AjaxResult.error());
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    protected String toAjax(boolean result) {
        return result ? success() : error();
    }

}
